import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: null,
    userInfo: null,
    cartList: null,
    likeList: null,
    likeDetail: null
  },
  getters: {
    token(state) {
      let t = state.token;
      if (!t) {
        t = localStorage.getItem('token');
      }
      return t;
    },
    userInfo(state) {
      let t = state.userInfo;
      if (!t) {
        t = JSON.parse(localStorage.getItem('userInfo'));
      }
      return t;
    },
    cartList(state) {
      let t = state.cartList;
      if (!t) {
        t = JSON.parse(localStorage.getItem('cartList'));
      }
      return t;
    },
    likeDetail(state) {
      let t = state.likeDetail;
      if (!t) {
        t = localStorage.getItem('likeDetail');
      }
      return t;
    },
    likeList(state) {
      let t = state.likeList;
      if (!t) {
        t = localStorage.getItem('likeList');
        if(t){
          t = t.split(',')
        }
      }
      return t;
    }
  },
  mutations: {
    login(state, payload) {
      state.token = payload;
      localStorage.setItem('token', payload);
      document.cookie = "token=" + payload
    },
    setUser(state, payload) {
      state.userInfo = payload
    },
    likeList(state, payload) {
      state.likeList = payload
    },
    loginOut(state) {
      state.token = null;
      state.userInfo = null;
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      localStorage.removeItem('cartList')
      localStorage.removeItem('likeList')
      document.cookie = 'token=""';
    },
    setCart(state, payload) {
      for (let i = 0; i < payload.length; i++) {
        payload[i].truePrice = payload[i].truePrice.toFixed(2);
      }
      state.cartList = payload
    },
    likeDetail(state, payload) {
      state.likeDetail = payload;
    }
  },
  actions: {
    getLoginUser(context) {
      axios({
        method: "get",
        url: "http://47.115.51.185/api/user",
        headers: {
          'Authori-zation': 'Bearer ' + this.getters.token
        }
      }).then(d => {
        if (d.data.status == 200) {
          console.log("获取信息==>", d)
          context.commit('setUser', d.data.data);
          localStorage.setItem('userInfo', JSON.stringify(d.data.data));
        }
      })
    },
    getCartList(context) {
      axios({
        method: "get",
        url: "http://47.115.51.185/api/cart/list",
        headers: {
          'Authori-zation': 'Bearer ' + this.getters.token
        }
      }).then(d => {
        if (d.data.status == 200) {
          // console.log("getCartList==>", d)
          context.commit('setCart', d.data.data.valid);
          localStorage.setItem('cartList', JSON.stringify(d.data.data.valid));
        }
      })
    },
    getLikeList(context) {
      axios({
        method: "get",
        url: 'http://47.115.51.185/api/collect/user?page=1&limit=20',
        headers: {
          'Authori-zation': 'Bearer ' + this.getters.token
        }
      }).then(d => {
        if (d.data.status == 200) {
          let likeArr = [];
          for (let i = 0; i < d.data.data.length; i++) {
            likeArr.push(d.data.data[i].pid)
          }
          context.commit('likeList', likeArr);
          context.commit('likeDetail', d.data.data);
          localStorage.setItem('likeList', likeArr)
          localStorage.setItem('likeDetail', d.data.data)
        } else {
          console.log(d.data.msg)
        }
      })
    }
  },
  modules: {}
})